//
//  SearchTableViewController.m
//  HeXun
//
//  Created by ibokan on 15/11/23.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "SearchTableViewController.h"


@interface SearchTableViewController ()<UISearchBarDelegate, UISearchResultsUpdating>
{
    //搜索数据源
    NSMutableArray *_names;
    
    //搜索结果数组
    NSMutableArray *_resultData;
    
    
    
    //存放股票信息的字典
    NSMutableDictionary *_stockInfo;
    
}
@end

@implementation SearchTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    _names = [NSMutableArray new];
    _stockInfo = [NSMutableDictionary new];
    
    //从本地文件拿到所有股票信息
    [self getStockInfo];
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchController.searchBar.text = self.searchBarText;
    self.searchController.searchBar.keyboardType = UIKeyboardTypeNumberPad;
    
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _resultData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"name";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
    }
    
    
    cell.textLabel.text = _resultData[indexPath.row];
    cell.detailTextLabel.text = _stockInfo[_resultData[indexPath.row]];
    
    return cell;
}


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = [self.searchController.searchBar text];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    
    if (_resultData != nil)
    {
        [_resultData removeAllObjects];
    }
    
    //过滤数据
    _resultData = [NSMutableArray arrayWithArray:[_names filteredArrayUsingPredicate:predicate]];
    
    //刷新表格
    [self.tableView reloadData];
}

#pragma mark - 从本地文件拿到所有股票信息
-(void)getStockInfo
{
    //构建文件路径
    NSString *localstockPath = [[NSBundle mainBundle] pathForResource:@"localstock" ofType:@"plist"];
    
    //读取数据，得到字典
    NSDictionary *resultDict = [NSDictionary dictionaryWithContentsOfFile:localstockPath];
    
    //从字典中拿出所有的值存入数组中
    NSArray *allStockInfo = resultDict.allValues;
    
    //循环读取数组中的对象，且赋值给模型
    for (NSInteger i = 0; i < allStockInfo.count; ++i)
    {
        //把股票编码赋值给表视图搜索数据源
        [_names addObject:allStockInfo[i][1]];
        
        //把股票信息存入字典，股票编码为Key，股票名称为Object
        [_stockInfo setObject:allStockInfo[i][2] forKey:allStockInfo[i][1]];
        
    }
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
